$(function() {
	// when < ie8, add function indexOf to object Array.
	if (!Array.prototype.indexOf) {
		Array.prototype.indexOf = function(elt /* , from */) {
			var len = this.length >>> 0;
			var from = Number(arguments[1]) || 0;
			from = (from < 0) ? Math.ceil(from) : Math.floor(from);
			if (from < 0)
				from += len;
			for (; from < len; from++) {
				if (from in this && this[from] === elt)
					return from;
			}
			return -1;
		};
	}

	var options = {
		language : 'zh-CN',
		weekStart : 1,
		autoclose : true,
		todayBtn : true,
		todayHighlight : true,
		forceParse : true,
		pickerPosition : "bottom-right"
	};

	// calendar click event
	$('input.suredy-datetimepicker ~ .btn').has('.icon-calendar').on('click', function(event) {
		$('.suredy-datetimepicker', $(this).parent()).datetimepicker('hidden').datetimepicker('show');
	});

	// remove click event
	$('input.suredy-datetimepicker ~ .btn').has('.icon-remove').on('click', function(event) {
		$('.suredy-datetimepicker', $(this).parent()).val('');
	});

	// input click event
	$('input.suredy-datetimepicker').on('click', function(event) {
		$(this).datetimepicker('hidden').datetimepicker('show');
	});

	// hour picker
	$('input.suredy-datetimepicker.hour').each(function() {
		var $this = $(this);

		var opts = $.extend({}, options, {
			format : 'hh:ii',
			startView : 'day',
			minView : 'hour',
			minuteStep : 1,
			initialDate : $this.val() || new Date()
		});

		$this.datetimepicker(opts);
	});

	// day picker
	$('input.suredy-datetimepicker.day').each(function() {
		var $this = $(this);

		var opts = $.extend({}, options, {
			format : 'yyyy-mm-dd',
			startView : 'month',
			minView : 'month',
			initialDate : $this.val() || new Date()
		});

		$this.datetimepicker(opts);
	});

	// all picker
	$('input.suredy-datetimepicker.all').each(function() {
		var $this = $(this);

		var opts = $.extend({}, options, {
			format : 'yyyy-mm-dd hh:ii',
			startView : 'month',
			minView : 'hour',
			minuteStep : 1,
			initialDate : $this.val() || new Date()
		});

		$this.datetimepicker(opts);
	});
});